<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否实名认证" prop="isAuthenticated">
        <el-select v-model="queryParams.isAuthenticated" placeholder="请选择是否实名认证" clearable size="small">
          <el-option v-for="dict in dict.type.didi_is_authenticated" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上次提现时间" prop="lastWithdrawalTime">
        <el-date-picker clearable v-model="queryParams.lastWithdrawalTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择上次提现时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="等级ID" prop="levelId">
        <el-input v-model="queryParams.levelId" placeholder="请输入等级ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['driver:driver:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['driver:driver:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['driver:driver:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['driver:driver:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="driverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="司机ID" align="center" prop="id" width="80" />
      <el-table-column label="手机号" align="center" prop="phone" width="100" />
      <el-table-column label="姓名" align="center" prop="name" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDriverOrder(scope.row)">
            {{ scope.row.name }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname" width="150" />
      <el-table-column label="头像url" align="center" prop="avatar" width="150" />
      <el-table-column label="是否实名认证" align="center" prop="isAuthenticated" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.didi_is_authenticated" :value="scope.row.isAuthenticated"/>
        </template>
      </el-table-column>
      <el-table-column label="禁用状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <!-- <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
          <el-tag v-else size="small" type="success">正常</el-tag> -->
          <dict-tag :options="dict.type.didi_driver_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="余额" align="center" prop="balance" width="80" />
      <el-table-column label="上次提现时间" align="center" prop="lastWithdrawalTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastWithdrawalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="司机评分" align="center" prop="driverRating" width="80" />
      <el-table-column label="司机等级" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="handleLevelDetail(scope.row)">{{ scope.row.levelName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['driver:driver:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['driver:driver:remove']">删除</el-button>
          <el-button size="mini" type="text" :icon="scope.row.status === 0 ? 'el-icon-success' : 'el-icon-error'"
            @click="handleUpdateStatus(scope.row)" v-hasPermi="['driver:driver:edit']">
            {{ scope.row.status === 0 ? '启用' : '禁用' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改司机对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" type="password" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像url" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像url" />
        </el-form-item>
        <el-form-item label="司机评分" prop="driverRating">
          <el-input v-model="form.driverRating" placeholder="请输入司机评分" />
        </el-form-item>
        <el-form-item label="司机等级" prop="levelId">
          <el-input v-model="form.levelId" placeholder="请输入等级ID" />
        </el-form-item>
        <!-- <el-divider content-position="center">司机等级信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTLevel">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTLevel">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tLevelList" :row-class-name="rowTLevelIndex" @selection-change="handleTLevelSelectionChange"
          ref="tLevel">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="等级名称" prop="levelName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.levelName" placeholder="请输入等级名称" />
            </template>
          </el-table-column>
          <el-table-column label="等级最小分数" prop="minScore" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.minScore" placeholder="请输入等级最小分数" />
            </template>
          </el-table-column>
          <el-table-column label="等级最大分数" prop="maxScore" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.maxScore" placeholder="请输入等级最大分数" />
            </template>
          </el-table-column>
          <el-table-column label="抽成比例" prop="commissionRate" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.commissionRate" placeholder="请输入抽成比例" />
            </template>
          </el-table-column>
        </el-table> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="司机等级详细" :visible.sync="openLevelDetail" width="600px" append-to-body>
      <el-form ref="levelDetail" :model="levelDetail" label-width="120px" size="mini">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="等级id:">
              {{ levelDetail.id }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级名称:">
              {{ levelDetail.levelName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="等级最小分数:">
              {{ levelDetail.minScore }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级最大分数:">
              {{ levelDetail.maxScore }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="抽成比例:">
              {{ levelDetail.commissionRate }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog title="司机订单简要信息" :visible.sync="openDriverOrder" width="70vw" append-to-body>
      <el-table :data="driverOrderList" style="width: 100%" >
          <el-table-column prop="id" label="订单id" width="80" align="center"></el-table-column>
          <el-table-column prop="carId" label="车辆id" width="80" align="center"></el-table-column>
          <el-table-column prop="startPoint" label="起始地点" width="width" align="center"></el-table-column>
          <el-table-column prop="endPoint" label="目的地" width="width" align="center"></el-table-column>
          <el-table-column prop="startTime" label="订单开始时间" width="width" align="center"></el-table-column>
          <el-table-column prop="endTime" label="订单结束时间" width="width" align="center"></el-table-column>
          <el-table-column prop="actualPayment" label="实际支付金额(元)" width="width" align="center"></el-table-column>
      </el-table>
      <!-- <el-table :data="driverOrderList" border>
        <el-table-column label="订单id" align="center" prop="id" width="80" />
      </el-table> -->
    </el-dialog>

  </div>
</template>

<script>
import { listDriver, getDriver, delDriver, addDriver, updateDriver, getLevelDetail, getDriverOrderList } from "@/api/didi/driver";

export default {
  name: "Driver",
  // 载入字典
  dicts: ['didi_driver_status', 'didi_is_authenticated'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTLevel: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 司机表格数据
      driverList: [],
      // 司机订单表格数据
      driverOrderList: [],
      // 司机等级表格数据
      tLevelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示等级详情弹出层
      openLevelDetail: false,
      // 等级详情
      levelDetail: {},
      // 司机订单
      driverOrderList: [],
      // 司机订单弹出层
      openDriverOrder: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        password: null,
        name: null,
        nickname: null,
        avatar: null,
        isAuthenticated: null,
        status: null,
        balance: null,
        lastWithdrawalTime: null,
        driverRating: null,
        levelId: null,
        levelName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询司机列表 */
    getList() {
      this.loading = true;
      listDriver(this.queryParams).then(response => {
        this.driverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        password: null,
        name: null,
        nickname: null,
        avatar: null,
        isAuthenticated: null,
        createTime: null,
        updateTime: null,
        status: null,
        balance: null,
        lastWithdrawalTime: null,
        driverRating: null,
        levelId: null
      };
      this.tLevelList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加司机";
    },

    handleDriverOrder(row) {
      const id = row.id
      this.driverOrderList = [];
      this.openDriverOrder = true;
      this.title = "司机订单";
      getDriverOrderList(id).then(response => {
        this.driverOrderList = response.rows;
      });
    },

    /** 司机等级详情按钮操作 */
    handleLevelDetail(row) {
      const id = row.levelId
      getLevelDetail(id).then(response => {
        this.levelDetail = response.data;
        this.openLevelDetail = true;
        this.title = "司机等级详情";
      });
    },
    /** 司机状态修改 */
    handleUpdateStatus(row) {
      this.reset();
      const id = row.id
      this.$modal.confirm(row.status === 1 ? `是否确认停用编号为"${id}"的司机` : `是否确认启用编号为"${id}"的司机`).then(() => {
        this.form.id = id;
        row.status === 1 ? this.form.status = 0 : this.form.status = 1;
        updateDriver(this.form).then(response => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        });
      }).catch(() => { });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDriver(id).then(response => {
        this.form = response.data;
        this.tLevelList = response.data.tlevelList;
        this.open = true;
        this.title = "修改司机";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tLevelList = this.tLevelList;
          if (this.form.id != null) {
            updateDriver(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDriver(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除司机编号为"' + ids + '"的数据项？').then(function () {
        return delDriver(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 司机等级序号 */
    rowTLevelIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 司机等级添加按钮操作 */
    handleAddTLevel() {
      let obj = {};
      obj.levelName = "";
      obj.minScore = "";
      obj.maxScore = "";
      obj.commissionRate = "";
      this.tLevelList.push(obj);
    },
    /** 司机等级删除按钮操作 */
    handleDeleteTLevel() {
      if (this.checkedTLevel.length == 0) {
        this.$modal.msgError("请先选择要删除的司机等级数据");
      } else {
        const tLevelList = this.tLevelList;
        const checkedTLevel = this.checkedTLevel;
        this.tLevelList = tLevelList.filter(function (item) {
          return checkedTLevel.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTLevelSelectionChange(selection) {
      this.checkedTLevel = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('driver/driver/export', {
        ...this.queryParams
      }, `driver_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
